/**
 * @file     addBinary.js
 * @brief    [67. 二进制求和](https://leetcode.cn/problems/add-binary/)
 * @author   Zhu
 * @date     2022-10-15 02:14
 */

/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function (a, b) {
    const lenA = a.length;
    const lenB = b.length;
    const maxLen = Math.max(lenA, lenB);
    let cur = 0;
    let carry = 0;
    let res = '';
    while (cur < maxLen) {
        cur++;
        let curA = +(a[lenA - cur] ?? 0);
        let curB = +(b[lenB - cur] ?? 0);

        let sum = carry + curA + curB;
        // 1.
        res = (sum & 1) + res;
        carry = sum >>> 1;
        // 2.
        // res = (sum % 2) + res;
        // carry = Math.floor(sum / 2);
    }

    return carry ? carry + res : res;
};
